#include "game.h"
void menu()
{
	printf("**************************************\n");
	printf("**********欢迎来到扫雷系统************\n");
	printf("**********1.开始游玩扫雷**************\n");
	printf("**********0.退出扫雷系统**************\n");
	printf("**************************************\n");
}
void menu2()
{
	printf("**************************************\n");
	printf("**********1.排查操作******************\n");
	printf("**********2.插旗子操作****************\n");
	printf("**********3.取消棋子操作**************\n");
	printf("**************************************\n");
}
void game()
{
	int input = 0;			//选择执行操作
	int ret = 0;			//接受返回的win判断是否排查完毕数组格子
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	//初始化二维数组
	init_Mine(mine, ROWS, COLS);
	init_Show(show, ROWS, COLS);
	//打印数组
	//print_Show(show, ROW, COL);
	//放置雷
	set_mine(mine, ROW, COL);
	print_Show(show, ROW, COL);
	//排查雷
	printf("\n");
	do {
		menu2();
		printf("\n请选择>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			ret = find_Mine(mine, show, ROW, COL);
			break;
		case 2:
			Flag(show, ROW, COL);
			break;
		case 3:
			Cancel_flag(show, ROW, COL);
			break;
		default:
			printf("输入错误，请重新选择");
		}
	} while (ret);
}	
//初始化布置雷的数组
void init_Mine(char mine[ROWS][COLS], int rows,int cols)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			mine[i][j] = '0';
		}
	}
}
//初始化显示数组
void init_Show(char show[ROWS][COLS], int rows, int cols)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			show[i][j] = '*';
		}
	}
}
//打印数组操作
void print_Show(char show[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <=row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", show[i][j]);
		}
		printf("\n");
	}
}
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)	
{

	int count = MINE;
	while (count)
	{
		int x = rand() % ROW + 1;
		int y = rand() % COL + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}
//排查操作
int find_Mine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)	

{
	int x = 0, y = 0;
	int win = 0;
	if (win < row * col - MINE)				//最多可排查次数为row*col-MINE,达到最后一次排查跳出
	{
		printf("请输入排查坐标>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)		//判断坐标合法性
		{
			if (mine[x][y] == '1') 
			{
				printf("哈哈哈哈恭喜您上天啦,GAME OVER!!!!!!!\n");
				print_Show(mine, ROW, COL);
				return 0;
			}
			else
			{
				Boom_board(show, mine, x, y);
				print_Show(show, ROW, COL);
				win = Browse_show(show, ROW, COL);
			}
		}
		else
		{
			printf("输入坐标非法，请重新输入\n");
		}
	}
	if (win == row * col - MINE)
	{
		printf("可恶您竟然赢了T-T\n");
		print_Show(mine, ROW, COL);
	}
	return win;
}
//获得9宫格内雷的数目
int get_count(char mine[ROWS][COLS], int x, int y)							
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
}
//打开数组格子操作，若9宫格没有雷即0则递归操作炸开一片区域
int Boom_board(char show[ROWS][COLS],char mine[ROWS][COLS], int x, int y)			
{
	if (x<1 || x>ROW || y<1 || y>COL)				//判定越界问题
	{
		return 0;
	}
	if (show[x][y] == '*')							//判断是否排查
	{
		int count = get_count(mine, x, y);			
		show[x][y] = count + '0';					//让该格子显示9宫格内雷的数目
		if (show[x][y] == '0')
		{	
			show[x][y] = ' ';						//周围无雷转化为空白字符
			//递归9宫格周围的格子
			Boom_board(show, mine, x - 1, y - 1);
			Boom_board(show, mine, x - 1, y);
			Boom_board(show, mine, x - 1, y + 1);
			Boom_board(show, mine, x, y - 1);
			Boom_board(show, mine, x, y);
			Boom_board(show, mine, x, y + 1);
			Boom_board(show, mine, x + 1, y - 1);
			Boom_board(show, mine, x + 1, y);
			Boom_board(show, mine, x + 1, y + 1);
		}
		else
			return 0;
	}
	else
		return 0;
}
//遍历显示数组
int Browse_show(char show[ROWS][COLS], int row, int col)			
{
	int win = 0;				//记录已排查格子个数
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= col; j++)
		{
			if (show[i][j] != '*')					
			{
				win++;
			}
		}
	}
	return win;
}
//标记旗子函数
int Flag(char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	static count = 0;			//代表棋子数量，不能超过雷的数目
	if (count <= MINE)
	{
		printf("请输入插入旗子的坐标>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)			//判断坐标合法性
		{
			if (show[x][y] == '*')
			{
				show[x][y] = '!';
				count++;
			}
			else
			{
				printf("该处已排查，无法插旗\n");
			}
		}
	}
	if (count > MINE)
	{
		printf("旗子已满，无法继续标记\n");
	}
	print_Show(show, ROW, COL);
	return 0;
}
//取消旗子标记函数
int Cancel_flag(char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	printf("请输入坐标>");
	scanf("%d %d", &x, &y);
	if (x >= 1 && x <= row && y >= 1 && y <= col)			//判断坐标合法性
	{
		if (show[x][y] == '!')
		{
			show[x][y] = '*';
		}
		else
		{
			printf("该处未标记，无法取消旗子\n");
		}
	}
	print_Show(show, ROW, COL);
	return 0;
}